/*
 * $Id: Plugin.java 162 2009-05-22 15:52:22Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.gui.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;

import deltadesk.gui.swing.display.FullscreenWindow;
import deltadesk.gui.swing.widget.AnalogClock;
import deltadesk.gui.swing.widget.InfoBar;
import deltadesk.gui.swing.widget.Text;
import deltadesk.gui.swing.widget.box.Box;


/**
 * Deltadesk Swing plugin.
 * 
 * @author Andreas Meyer
 * @version $Revision: 162 $
 */
public final class Plugin extends org.java.plugin.Plugin {

	/** This plug-in ID. */
	public static final String PLUGIN_ID = "deltadesk.gui.swing";

	/** Our main window. */
	private FullscreenWindow window;

	/**
	 * @see org.java.plugin.Plugin#doStart()
	 */
	protected void doStart() throws Exception {
		log.info("+DeltaDesk.Swing");
		
		final JPanel panel = new JPanel();
		panel.setBackground(new Color(89, 87, 75));
		panel.setLayout(new BorderLayout());
		panel.setDoubleBuffered(true);
		
		Box box = new Box();
		box.setPreferredSize(new Dimension(1, 25));
		panel.add(box, BorderLayout.NORTH);
		box.add(new InfoBar());
		
		final JPanel contentPanel = new JPanel();
		panel.add(contentPanel, BorderLayout.CENTER);
		contentPanel.setBackground(new Color(89, 87, 75));
		contentPanel.setLayout(new GridLayout(2, 3, 5, 5));
		
		box = new Box();
		Text l = new Text("Welcome");
		box.add(l);
		contentPanel.add(box);

		box = new Box();
		AnalogClock clock = new AnalogClock();
		box.add(clock);
		contentPanel.add(box);

		contentPanel.add(new Box());
		contentPanel.add(new Box());
		contentPanel.add(new Box());

		box = new Box();
		box.setLayout(new GridLayout());
		box.setPreferredSize(new Dimension(1, 25));
		panel.add(box, BorderLayout.SOUTH);

		window = new FullscreenWindow(true);
		registerKeyListener();
		
		final String name = getDescriptor().getAttribute("app.name").getValue();
		window.show(0, name, panel);
		
		window.addKeyListener(new KeyAdapter() {

			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_N) {

					final Box box = new Box();
					Text l = new Text("Welcome");
					box.add(l);
					contentPanel.add(box);
					contentPanel.validate();
				} else if (e.getKeyCode() == KeyEvent.VK_1) {
					((GridLayout)contentPanel.getLayout()).setRows(10);
					contentPanel.validate();
				}
			}
		});

		log.info("*DeltaDesk.Swing");
	}

	/**
	 * 
	 * @see org.java.plugin.Plugin#doStop()
	 */
	protected void doStop() throws Exception {
		log.info("-DeltaDesk.Swing");
		
		window.hide();
		
		log.info("_DeltaDesk.Swing");
	}

	/**
	 * Handle key events.
	 * 
	 */
	private void registerKeyListener() {

		window.addKeyListener(new KeyAdapter() {

			public void keyPressed(final KeyEvent e) {

				// Handle Ctrl-C or ESC for exiting the application
				if (e.getKeyCode() == KeyEvent.VK_C && e.isControlDown()
						|| e.getKeyCode() == KeyEvent.VK_ESCAPE) {

					log.info("Shutdown initiated");
					getManager().shutdown();
				} else if(e.getKeyCode() == KeyEvent.VK_F12){
					window.toggleFullscreen();
				}
			}
		});
	}
}
